package com.springcloud.travel.theme.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;


import com.spingcloud.travel.common.JsonResult;
import com.spingcloud.travel.common.TableResult;
import com.springcloud.travel.entity.Order;
import com.springcloud.travel.entity.Theme;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.springcloud.travel.theme.service.ThemeService;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/theme")
public class ThemeController {
    @Autowired
    private ThemeService themeService;
    @Autowired
    private ObjectMapper objectMapper;

    /**
     * 主题管理的查询
     */
    @RequestMapping("/show")
    public TableResult<Theme> selectAllTheme(@RequestParam Map<String, String> map){
        String page = map.get("page");
        String limit = map.get("limit");

        Page<Theme> pages = new Page<>();
        pages.setCurrent(Integer.parseInt(page));
        pages.setSize(Integer.parseInt(limit));

        //分页查询
        Page<Theme> themePage = themeService.page(pages,this.getQueryWrapper(map));
        if(themePage.getRecords()!=null&&themePage.getRecords().size()>0){
            return TableResult.getSuccess((int)themePage.getTotal(), themePage.getRecords());
        }
       return null;

    }

    /**
     *
     * @param map
     * @return
     */
    private QueryWrapper getQueryWrapper(Map<String, String> map) {
            QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
            String themeType = (String) map.get("themeType");
//            String orderState = (String) map.get("state");
            // 判断是否为空
            if (!StringUtils.isEmpty(themeType)){
                queryWrapper.like("theme_type",themeType);
            }
//            if (!StringUtils.isEmpty(orderState)){
//                queryWrapper.like("order_state",orderState);
//            }
            return queryWrapper;

    }


    /**
     * 主题管理的增加
     */
    @RequestMapping("/add")
    public boolean add(@RequestBody Theme theme) throws JsonProcessingException {
        System.out.println("add"+theme);
        boolean insert = themeService.save(theme);

        return insert;
    }

    /**
     * 主题管理的修改
     */
    @RequestMapping("/update")
    public boolean update(@RequestBody Theme theme){
        System.out.println("update"+theme);
        boolean updateById = themeService.updateById(theme);
        return updateById;
    }



    /**
     * 主题管理的删除
     */
    @RequestMapping("/delete")
    public boolean delete(@RequestParam("ids") String[]  ids){

        boolean deleteById = themeService.removeByIds(Arrays.asList(ids));
        return deleteById;
    }


}
